//package com.zomiot.portal.aichat.config;
//
//import jakarta.annotation.PostConstruct;
//import lombok.RequiredArgsConstructor;
//import org.springframework.ai.embedding.Embedding;
//import org.springframework.ai.embedding.EmbeddingClient;
//import org.springframework.ai.embedding.EmbeddingRequest;
//import org.springframework.ai.embedding.EmbeddingResponse;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.*;
//import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.List;
//import java.util.Map;
//
///**
// * @ClassName OpenRouterEmbeddingClient
// * @Author 阿⭐
// * @Date 2025/11/5 16:31
// * @Description TODO
// */
//// src/main/java/com/company/ai/OpenRouterEmbeddingClient.java
//@Component
//@RequiredArgsConstructor
//public class OpenRouterEmbeddingClient implements EmbeddingClient {
//
//    @Value("${spring.ai.openai.api-key}")
//    private String apiKey;
//
//    private final RestTemplate restTemplate = new RestTemplate();
//
//    @PostConstruct
//    public void init() {
//        restTemplate.getMessageConverters().add(0, new MappingJackson2HttpMessageConverter());
//    }
//
//    @Override
//    public EmbeddingResponse call(EmbeddingRequest request) {
//        String url = "https://openrouter.ai/api/v1/embeddings";
//
//        Map<String, Object> body = Map.of(
//                "model", "nomic-ai/nomic-embed-text-v1.5",
//                "input", request.getInput()
//        );
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.set("Authorization", "Bearer " + apiKey);
//        headers.set("HTTP-Referer", "http://localhost:8080");
//        headers.set("X-Title", "Enterprise Portal AI");
//
//        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(body, headers);
//
//        try {
//            ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
//            Map<String, Object> respBody = response.getBody();
//
//            List<Map<String, Object>> data = (List<Map<String, Object>>) respBody.get("data");
//            List<Embedding> embeddings = data.stream()
//                    .map(item -> new Embedding((List<Double>) item.get("embedding"), (Integer) item.get("index")))
//                    .toList();
//
//            return new EmbeddingResponse(embeddings);
//        } catch (Exception e) {
//            throw new RuntimeException("Embedding failed: " + e.getMessage(), e);
//        }
//    }
//}